package game.pathfinding;

import game.TileMap;

import java.util.Vector;

public class Path
{
	public Vector<PathElement> stops = new Vector<PathElement>();
	public int totalCost = 0;
	public PathElement lastFit = null;
	public boolean found = false;
	
	public PathElement getElementAt(int x, int y)
	{
		for(PathElement pe : stops)
			if(pe.equals(x, y)) return pe;
		return null;
	}
	
	public boolean advanceTo(int x, int y)
	{
		PathElement e = getElementAt(x, y);
		if(e == null)
			return false;
		else
			return advanceTo(e);
	}
	
	public boolean advanceTo(PathElement pe)
	{
		if(!stops.contains(pe)) return false;
		for(int i = 0; i < stops.size(); ++i)
		{
			if(stops.get(i).equals(pe))
			{
				stops.remove(i);
				return true;
			}
			else
			{
				stops.remove(i);
				i--;
			}
		}
		return false;
	}
	
	public void recheckCost(int available, TileMap map)
	{
		boolean end = false;
		for(PathElement pe : stops)
		{
			if(available >= map.getTileAt(pe.X, pe.Y).getMovPenalty() && !end)
			{
				available -= map.getTileAt(pe.X, pe.Y).getMovPenalty();
				pe.fits = true;
			}
			else
			{
				end = true;
				pe.fits = false;
			}
		}
	}
}